package com.laikan.reader.core.adapter.holder;

import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.laikan.reader.core.LaikanApplication;
import com.laikan.reader.R;
import com.laikan.reader.core.base.BaseHolder;
import com.laikan.reader.database.Book;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.core.view.SimpleTagImageView;

/**
 * 书架封面模式普通item
 * Created by Administrator on 2016/3/22.
 */
public class GridHolder extends BaseHolder<Book> {

    private SimpleTagImageView book_pic;
    private TextView tv_name;
    private TextView tv_record_status;
    private ImageView mSecletView = null;
    private ImageView mSelected = null;
    private View shadow;
    private boolean mLongPress;

    @Override
    public View initView() {
        view = LayoutInflater.from(LaikanApplication.getInstance().getApplicationContext()).inflate(R.layout.books_grid_item, null);
        book_pic = (SimpleTagImageView)view.findViewById(R.id.book_pic);
        tv_name = (TextView)view.findViewById(R.id.top_book_name);
        tv_record_status = (TextView)view.findViewById(R.id.tv_read_status);
        mSecletView = (ImageView)view.findViewById(R.id.select);
        mSelected = (ImageView)view.findViewById(R.id.selected);
        shadow = (View)view.findViewById(R.id.shadow);
        return view;
    }

    @Override
    public void refreshView() {
        Book book = getData();
        if (book == null) {
            return;
        }

        // 1.书的封面
        book_pic.setImageURI(Uri.parse(book.getIcon()));
        // 2.书名
        tv_name.setText(book.getName());

        // 3.该书籍当前阅读进度
        int chapCount = SPUtil.getInt("chap_count_" + book.getId(), 0);
        int chapPos = SPUtil.getInt(book.getId() + "chaptep_rec", -1);
        if (chapCount != 0 && chapPos != -1) {
            double shelf_index = (SPUtil.getInt(book.getId() + "shelf_index", 0) + 1);
            double readchap = shelf_index / chapCount * 100;
            //  int readchap = (int)readchap;
            //  String s = String.format("%.2f", readchap);
            if ((int)readchap == 100) {
                tv_record_status.setText("已读完");
            }else {
                tv_record_status.setText("已读：" + (int)readchap + "%");
            }
            //  tv_record.setText("已读："+(SPUtil.getInt(b.getId() + "shelf_index", 0) + 1) + "/" + chapCount);
        }else {
            tv_record_status.setText("未读");
        }
/*
        KLog.e("uptade: "+ SPUtil.getBoolean("isUpdata_" + book.getId(), false) +"  "+book.getStatus() +"  "+book.getId() +"   "+ book.getName());
        4.设置书籍右上角标签文字
        */
        if (SPUtil.getBoolean("isUpdata_" + book.getId(), false) && !"完本".equals(book.getStatus())) {
            // 更新
            book_pic.setTagWidth(15);
            book_pic.setTagText("更新");
        }else {
            book_pic.setTagWidth(0);
            book_pic.setTagText("");
        }
        // 5.设置书籍右上角标签 背景色
        setTagColor(book_pic, (String)book_pic.getTagText());
    }

    /**
     * 设置书籍右上角标签背景色
     *
     * @param pic
     * @param tag
     */

    private void setTagColor(SimpleTagImageView pic, String tag) {
        if ("更新".equals(tag)) {
            pic.setTagBackgroundColor(LaikanApplication.applicationContext.getResources().getColor(R.color.C_88BB00));
        }else if ("限免".equals(tag)) {
            pic.setTagBackgroundColor(LaikanApplication.applicationContext.getResources().getColor(R.color.C_3399FF));
        }else if ("一折".equals(tag)) {
            pic.setTagBackgroundColor(LaikanApplication.applicationContext.getResources().getColor(R.color.C_EE0022));
        }
    }

}
